Page({
    data: {
        xPageReady : true ,
        xQuiz : [] ,
        xCurrent : 0 ,
        xWeek : '' ,
        xWidth : 0 ,
        product_id:'',
        treatment_item_id:''
    },

    onLoad: function (options) {
        this.setData({
            xWeek : Number(options.week),
            treatment_item_id:options.treatment_item_id,
        })
        this.xInit( Number(options.week))
    },
    xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xInfo();//触发父页面中的方法
      }
    },
    //  初始化
    xInit: async function (week) {
        let that = this
        wx.cloud.callFunction({
            name: 'quiz_question',
            data: {
                action: "get_quiz_question_by_week",
                dataObj: {
                    week: week
                    // breakthrough_point: state ? "after_teaching_reading" : "end_phase"
                }
            }
        }).then(res => {
            that.setData({
              xQuiz : res.result
            })
        })
				
        that.setData({
            xPageReady : false
        })
    },

    // 选择项
    xSelect: async function (e) {
        let that = this
        wx.showLoading({
            title: '请稍等',
        })
        let quiz = that.data.xQuiz
        let week = that.data.xWeek
        let params = e.currentTarget.dataset
        let index = params.index
        let key = params.key
        
        for(var i in quiz[index].options){
            quiz[index].options[i].select = false
        }

        quiz[index].options[key].select = true
        quiz[index].sel = true


        /* 记录单题选择 */
        let option = quiz[index].options

        let ans = option.find( ans => { return ans.is_correct_answer == 1 })

        let answer = {
            choose_answer : [
                {
                    question_id : quiz[index]._id ,
                    answer : quiz[index].options[key].id ,
                    right_key : ans.id
                }
            ] ,
            week : week ,
            type : 1 ,
            treatment_item_id:that.data.treatment_item_id
        }
        await wx.cloud.callFunction({
            name: 'quiz_question',
            data: {
                action: "add_quiz_result",
                dataObj: answer
            }
        })

        /* 进度条计算 */

        let proce = quiz.length

        let w = ( ( index + 1 ) / proce) * 100

        that.setData({
            xQuiz : quiz ,
            xWidth : w
        })

        wx.hideLoading()
    },

    // 禁止滑动
    xCatchtouchMove: function () {
        return false
    },

    // 下一题
    xNext: function (e) {
        let that = this
        let index = e.currentTarget.dataset.key

        that.setData({
            xCurrent : index + 1
        })
    },

    // 返回时提交答案
    xBack: async function () {
        let that = this
        wx.showLoading({
            title: '请稍等',
        })
        let quiz = that.data.xQuiz
        let week = that.data.xWeek
        let day =that.data.day
        let answer = {
            choose_answer : [] ,
            week : week ,
            treatment_item_id:that.data.treatment_item_id,
            type : 2 ,
            day:day
        }

        for(var i in quiz){
            let xid = quiz[i]._id
            let option = quiz[i].options
            let key = option.find( key => { return key.is_correct_answer == 1 })
            let sel = option.find( sel => { return sel.select == true })
            answer.choose_answer.push({
                question_id : xid ,
                answer : sel.id ,
                right_key : key.id
            }) 
        }

        await wx.cloud.callFunction({
            name: 'quiz_question',
            data: {
                action: "add_quiz_result",
                dataObj: answer
            }
        })
        that.xChildReload()
        wx.redirectTo({
          url: '../../techevlR/index/index?total='+JSON.stringify(that.data.xQuiz),
        })
    }
})